import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import electron from 'vite-plugin-electron'
import renderer from 'vite-plugin-electron-renderer'
import path from 'path'
// 引入插件和解析器
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig({
  base: './',
  plugins: [
    vue(),
    // Element Plus 自动导入
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    // Electron集成配置
    electron([
      {
        entry: 'electron/main.js',
        onstart(options) {
          options.startup()
        },
      },
      {
        entry: 'electron/preload.js',
        onstart(options) {
          // 预加载脚本将在主进程重新加载时重新打包
          options.reload()
        },
      },
    ]),
    // 使用详细的renderer配置
    renderer({
      resolve: {
        fs: true,
        path: true,
        electron: true
      },
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  optimizeDeps: {
    include: [
      'element-plus/es/components/button/index.mjs',
      'element-plus/es/components/input/index.mjs',
      'element-plus/es/components/select/index.mjs',
      'element-plus/es/components/option/index.mjs',
      // 可以根据需要添加更多组件路径
    ],
  },
  build: {
    outDir: 'dist',
    minify: 'terser',
    terserOptions: {
      compress: {
        // 移除日志和调试信息
        drop_console: false,
        drop_debugger: true,
      },
    },
  },
  server: {
    host: '0.0.0.0',
    port: 5173,
  },
})
